# coding:utf-8
from win32gui import GetWindowText, EnumWindows, PostMessage
from win32con import WM_CLOSE
import win32process

import os
import signal

WINDOW_TEXT = "微信"

def killProcess(pid):
	"""杀死进程"""
	try:
		a = os.kill(pid, signal.SIGINT)
		print 'Kill process pid: %s, return: %s...' % (pid, a)
	except OSError as e:
		print e

def closeWindow(hwnd):
	"""关闭窗口"""
	PostMessage(hwnd, WM_CLOSE, 0, 0)
	print 'Close window...'

def getWindowText(hwnd):
	"""获取窗口进程描述"""
	return GetWindowText(hwnd)

def closeCQ(hwnd, mouse):
	"""处理函数_关闭酷Q程序"""
	if getWindowText(hwnd) == unicode(WINDOW_TEXT, 'utf-8').encode('gbk'):
		closeWindow(hwnd)
		pids = win32process.GetWindowThreadProcessId(hwnd)
		print pids
		killProcess(pids[1])

if __name__ == '__main__':
	EnumWindows(closeCQ, 0)